package com.neusoft.utils.url;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.neusoft.utils.http.HttpUtil;
import com.neusoft.utils.json.JsonUtil;
import com.neusoft.utils.map.HashMap;
import com.neusoft.utils.map.MyHashMap;
import com.neusoft.utils.string.StringUtil;
import com.neusoft.utils.validate.ValidateUtils;

public class URLUtil {
	private static String CHARSET = "UTF-8";
	public static String decode(String url) {
		String result = null;
		try {
			result = URLDecoder.decode(url, CHARSET);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public static String encode(String url){
		String result = null;
		try {
			result = URLEncoder.encode(result, CHARSET);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}
	public static String formatURL(String url){
		url=url.trim();
		String tempUrl=url.toLowerCase();
		String pre=StringUtil.substringBefore(tempUrl, "://");
		if(ValidateUtils.en_Num(pre)){
			return url;
		}
	    return "http://"+url;	
	};
    public static String toShortURL(String url){
       url=formatURL(url);
       Map<String,String> param= new HashMap<String,String>();
 	   param.put("source", "3271760578");
 	   param.put("url_long", url);
 	    String result= HttpUtil.doGet("http://api.t.sina.com.cn/short_url/shorten.json", param); 
 	    if(StringUtil.isNull(result)){
 	    	return null;
 	    }
 	    List<Map<String,String>>  list=	    JsonUtil.fromJson(result, new TypeReference<List<Map<String,String>>>() {
		});
 	    if(list.isEmpty()){
 	    	return null;
 	    }
 	    return list.get(0).get("url_short");
    }
   public static void main(String[] args) {
	   Map<String,String> param2=new HashMap<String,String>().set("username", "24704687@qq.com")
			   .set("password", "change2012").set("captcha", null);
	  String result2= HttpUtil.doPost("http://www.juhe.cn/login/login",param2);
	  System.out.println(result2);
	   Map<String,String> param= new HashMap<String,String>();
	   param.put("requesttypesel", "GET");
	   param.put("params", JsonUtil.toJson(new MyHashMap().set("realName", "冯昌义").set("idcard", "370832199011020314")));
	  String result= HttpUtil.doPost("http://www.juhe.cn/box/newtest", param);
      System.out.println(result);

   }
}
